#include <vector>
#include "Box2dPolygonShape.h"
#include  <Box2D/Box2D.h>
#include <stdio.h>

namespace physics {

	Box2dPolygonShape::Box2dPolygonShape( Transform pos, b2PolygonShape* polygonShape ) :
	_pos( pos ),
	_polygonShape( polygonShape ) {
	}

	Transform Box2dPolygonShape::GetPos( ) const {
		return _pos;
	}

	void Box2dPolygonShape::SetPos( Transform pos ) {
		_pos = pos;
	}

	IIterable<Vec2>* Box2dPolygonShape::GetVertices( ) const {
		b2Vec2* b2Vertices = _polygonShape->m_vertices;
		int count = _polygonShape->m_vertexCount;

		Vector<Vec2>* vector = new Vector<Vec2 > ( );
		for( int i = 0; i < count; ++i ) {
			b2Vec2 vec = b2Vertices[i];
			vector->Add( Vec2( vec.x, vec.y ) );
		}
		return vector;
	}

	void Box2dPolygonShape::SetVertices( ICollection<Vec2>* vertices ) {
		int count = vertices->GetCount();

		int i = 0;
		b2Vec2* b2VerticesArray = new b2Vec2[count];
		for( IteratorPtr<Vec2> verticesIter( vertices->GetIterator( ) );
			!verticesIter->IsEnd( );
			verticesIter->Next( ) )
		{
			Vec2 vec = verticesIter->Current();
			b2VerticesArray[i].x = vec.X;
			b2VerticesArray[i].y = vec.Y;
			i++;
		}
		_polygonShape->Set( b2VerticesArray, count );

		delete b2VerticesArray;
	}

	const b2Shape* Box2dPolygonShape::GetBox2dShape( ) const {
		return _polygonShape;
	}

	void Box2dPolygonShape::Accept( const IShapeVisitor* visitor ) const {
		visitor->VisitPolygon(this);
	}

}
